#include <linux/fs.h>           
#include <linux/kernel.h>       
#include <linux/module.h>     
#include <linux/radix-tree.h>   // 基数树相关操作
#include <linux/slab.h>         
#include <linux/uaccess.h>     
#include <linux/file.h>       

#define MAX_PAGE_SIZE 4096  // 假设最大内存页面大小为 4KB

// 定义内存缓存的基数树根节点
struct radix_tree_root cache_tree;

// 读取文件内容并缓存到基数树的函数
static void Mread(const char *fn, unsigned long offset, size_t size) {
    struct file *file;           // 文件指针
    loff_t pos = offset;         // 偏移量
    char *buffer;                // 数据缓冲区
    int ret;                     // 返回值

    printk(KERN_INFO "Mread: Opening file %s for reading at offset %lu\n", fn, offset);

    // 为读取的数据分配内存
    buffer = kmalloc(size, GFP_KERNEL);
    if (!buffer) {
        printk(KERN_ERR "Mread: Memory allocation failed.\n");
        return;
    }

    // 打开文件（只读模式）
    file = filp_open(fn, O_RDONLY, 0);
    if (IS_ERR(file)) {
        printk(KERN_ERR "Mread: Failed to open file %s\n", fn);
        kfree(buffer);
        return;
    }

    // 从文件读取数据
    ret = kernel_read(file, buffer, size, &pos);
    if (ret < 0) {
        printk(KERN_ERR "Mread: Failed to read from file %s\n", fn);
        filp_close(file, NULL);
        kfree(buffer);
        return;
    }

    // 将读取的数据缓存到基数树中
    radix_tree_insert(&cache_tree, offset, buffer);

    printk(KERN_INFO "Mread: Read %zu bytes from file %s at offset %lu\n", size, fn, offset);

    // 打印读取的内容（限制为前128个字符，避免过多输出）
    printk(KERN_INFO "Mread: Read content: %.128s\n", buffer);

    // 关闭文件
    filp_close(file, NULL);
}

// 将数据写入文件
static void Mwrite(const char *fn, unsigned long offset, char *buff, size_t size) {
    struct file *file;           
    loff_t pos = offset;         
    int ret;                    

    printk(KERN_INFO "Mwrite: Opening file %s for writing at offset %lu\n", fn, offset);

    // 打开文件（写模式，如不存在则创建）
    file = filp_open(fn, O_WRONLY | O_CREAT, 0644);
    if (IS_ERR(file)) {
        printk(KERN_ERR "Mwrite: Failed to open file %s\n", fn);
        return;
    }

    // 向文件写入数据
    ret = kernel_write(file, buff, size, &pos);
    if (ret < 0) {
        printk(KERN_ERR "Mwrite: Failed to write to file %s\n", fn);
        filp_close(file, NULL);
        return;
    }

    printk(KERN_INFO "Mwrite: Wrote %zu bytes to file %s at offset %lu\n", size, fn, offset);

    filp_close(file, NULL);
}

// 模块初始化函数
static int __init buffer_init(void) {
    printk(KERN_INFO "Initializing buffer module...\n");

    // 初始化基数树
    INIT_RADIX_TREE(&cache_tree, GFP_KERNEL);

    printk(KERN_INFO "Buffer module loaded.\n");

    // 示例：调用 Mread 读取文件内容
    printk(KERN_INFO "Calling Mread...\n");
    Mread("/root/test.txt", 0, 128);  // 从文件偏移量 0 开始读取 128 字节

    // 示例：调用 Mwrite 写入文件内容
    printk(KERN_INFO "Calling Mwrite...\n");
    Mwrite("/root/test.txt", 128, "22222222222222", 14);  // 从偏移量 128 开始写入 14 字节

    return 0;
}

static void __exit buffer_exit(void) {
    struct radix_tree_iter iter; // 基数树迭代器
    void **slot;                 // 存储数据的槽位指针

    // 清理基数树：遍历并释放所有缓存的数据
    radix_tree_for_each_slot(slot, &cache_tree, &iter, 0) {
        kfree(radix_tree_deref_slot(slot));  // 释放槽位中的数据
    }

    printk(KERN_INFO "Buffer module unloaded.\n");
}

module_init(buffer_init);
module_exit(buffer_exit);

MODULE_LICENSE("GPL");  // 模块许可证
